package com.ithima.domin;

import java.sql.ClientInfoStatus;
import java.util.ArrayList;
import java.util.Scanner;

public class Student002 {
    public static void main(String[] args) {
        //1.创建一个学生类的集合
        ArrayList<Student> list = new ArrayList<>();
        //2.键盘录入
        Scanner sc = new Scanner(System.in);
        //3.死循环
        while (true) {
            System.out.println("------" + "欢迎来到学生管理系统" + "------");
            System.out.println("1.添加学生:");
            System.out.println("2.删除学生:");
            System.out.println("3.修改学生:");
            System.out.println("4.查看学生:");
            System.out.println("5.再见:");
            String zyl = sc.next();
            //swicth语句
            switch (zyl) {
                case "1":
                    System.out.println("添加学生");
                    tianJianStudent(list);
                    break;
                case "2":
                    System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case "3":
                    System.out.println("修改学生");
                    xiuGaiStudent(list);
                    break;
                case "4":
                    System.out.println("查看学生");
                    chanKanStudent(list);
                    break;
                case "5":
                    System.out.println("再见");
                    System.exit(0);
                    break;
                default:
                    System.out.println("你输入的数据有误,请重新输入:");
                    break;
            }
        }

    }

    //修改学生,也要判读索引==-1
    private static void xiuGaiStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        if (list.size() == 0) {
            return;
        }
        System.out.println("请输入要修改的学号");
        String xuehao;
        while (true) {
            xuehao = sc.next();
            int index = getIndex(list, xuehao);
            if (index == -1) {
                System.out.println("要修改的学号不存在,请重新输入");
            } else {
                System.out.println("请输入姓名");
                String name = sc.next();
                System.out.println("请输入年龄");
                int age = sc.nextInt();
                System.out.println("请输入生日");
                String shengri = sc.next();
                Student student = new Student(xuehao, name, age, shengri);
                list.add(index,student);
                System.out.println("修改成功");
                break;
            }
        }
    }

    //删除学生,要判读索引长度==-1
    private static void deleteStudent(ArrayList<Student> list) {
        //键盘录入要删除的学号
        Scanner sc = new Scanner(System.in);
        //先判断集合索引==0,返回
        if (list.size() == 0) {
            return;
        }
        String xuehao;
        //死循环
        while (true) {
            System.out.println("请输入要删除的学号");
            xuehao = sc.next();
            //调用getIndex方法
            int index = getIndex(list, xuehao);
            //如果索引==-1,那么输入的学号不存在,不是的话删除成功,结束循环
            if (list.size() == -1) {
                System.out.println("输入的学号不存在,重新输入");
            } else {
                list.remove(index);
                System.out.println("删除成功");
                break;
            }
        }
    }

    //查看学生信息,里面要判读索引长度<=0
    private static void chanKanStudent(ArrayList<Student> list) {
        //遍历数组,看看有没有学生信息
        for (int i = 0; i < list.size(); i++) {
            //list.get(i)
        }
        //如果索引长度<=0,那么暂无学生信息直接返回
        if (list.size() <= 0) {
            System.out.println("暂无学生信息请重新输入");
            return;
        }
        //显示表头信息
        System.out.println("学号\t姓名\t年龄\t生日");
        //遍历数组
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            //打印学生对象里面的变量
            System.out.println(student.getXuehao() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getShengri());
        }
    }

    //添加学生,要判断索引>=0
    private static void tianJianStudent(ArrayList<Student> list) {
        //1.用键盘录入添加学生
        Scanner sc = new Scanner(System.in);
        //2.设置Sting类型的学号
        String xuehao;
        //3.死循环
        while (true) {
            System.out.println("请输入学号");
            xuehao = sc.next();
            //4.调用判断学号是否存在的方法
            int index = getIndex(list, xuehao);
            //5.判断如果索引>=0,那么就是学号已经存在.不是的话结束判断
            if (index >= 0) {
                System.out.println("学号已经存在,请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String shengri = sc.next();
        //6.创建学生对象
        Student student = new Student(xuehao, name, age, shengri);
        //7.用添加的方法到集合中
        list.add(student);
        System.out.println("添加成功");
    }


    /*定义判断学号是否存在的方法
    1.有返回值
    2.int类型
    3.参数list,xuehao
     */
    public static int getIndex(ArrayList<Student> list, String xuehao) {
        //定义一个索引值为-1
        int index = -1;
        //遍历集合
        for (int i = 0; i < list.size(); i++) {
            //list.get(i)
            //如果键盘录入的学号==已经录入的学号
            if (xuehao.equals(list.get(i).getXuehao())) {
                //那么索引值=i
                index = i;
            }
        }
        return index;
    }
}
